import 'package:core/helper/log_helper.dart';
import 'package:flutter/material.dart';

class CoreNavigatorObserver extends NavigatorObserver {
  static final List<String> _history = [];

  static List<String> get history => _history;

  static void showHistory() {
    LogHelper.d(_history, title: 'routeHistory');
  }

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    //调用Navigator.of(context).push(Route()) 进栈时回调
    String? name = route.settings.name;
    if (null == name) return;
    LogHelper.d('入栈$name', title: '入栈提示');
    _history.add(name);
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    //调用Navigator.of(context).pop() 出栈时回调
    String? name = route.settings.name;
    if (null == name) return;
    LogHelper.d('出栈$name', title: '出栈提示');
    _history.remove(name);
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    //调用Navigator.of(context).removeRoute(Route()) 移除某个路由回调
    String? name = route.settings.name;
    if (null == name) return;
    LogHelper.d('出栈$name', title: '出栈提示');
    _history.remove(name);
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    //调用Navigator.of(context).replace( oldRoute:Route("old"),newRoute:Route("new")) 替换路由时回调
    String? oldName = oldRoute?.settings.name;
    String? newName = newRoute?.settings.name;
    if (null == oldName || null == newName) return;
    LogHelper.d('出栈$oldName,入栈$newName', title: 'replace出入桟提示');
    _history.remove(oldName);
    _history.add(newName);
  }

  @override
  void didStartUserGesture(Route<dynamic> route, Route<dynamic>? previousRoute) {
    ///iOS侧边手势滑动触发回调 手势开始时回调
  }

  @override
  void didStopUserGesture() {
    ///iOS侧边手势滑动触发停止时回调 不管页面是否退出了都会调用
  }
}
